﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace LMS.SMSH.WEB
{
    public partial class TeachOrder : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // 绑定实验室ID
                bingddlLab();
            }
        }

        /// <summary>
        /// 绑定实验室ID
        /// </summary>
        private void bingddlLab()
        {
            ddlLab.DataSource = LMSDataContext.GetLMSDB.TB_lab.Select(lab => lab.labId);
            ddlLab.DataBind();
        }


        /// <summary>
        /// 将预约信息写入数据库
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnOK_Click(object sender, EventArgs e)
        {
            #region 将预约实验室，预约实验时间，实验人数，写入TB_Lab数据表
            //获得单一结果的实验室实体类
            TB_lab tb_lab = LMSDataContext.GetLMSDB.TB_lab.Single(lab => lab.labId == ddlLab.SelectedValue); 

            switch (ddlLabStatus.SelectedValue)
            {
                case "1":
                    #region 上午一二节
                    #region 先检查该状态的实验室是否被预约
                    var query = LMSDataContext.GetLMSDB.TB_lab.Where(l => l.labId == ddlLab.SelectedValue && l.labStatus12 == true);
                    #endregion
                    if (query.Count() == 0)
                    {
                        // 表示该实验室在上午一二节可以被预约 
                        tb_lab.labStatus12 = true;
                        tb_lab.labStuNo = Int32.Parse(txtStuTotalCount.Text.Trim());
                        LMSDataContext.GetLMSDB.SubmitChanges();

                        #region 将实验室ID,预约时间,预约实验时间，教师ID，预约说明写入TB_orderLab数据表
                        WriteInfoTB_OrderLab();
                        #endregion

                        ClientScript.RegisterStartupScript(this.GetType(), "", "alert('预约成功!')", true);
                    }
                    else
                    {
                        //表示该实验室在上午一二节已经被预约了，不能冲突预约
                        ClientScript.RegisterStartupScript(this.GetType(), "", "alert('该实验室在上午一二节课已被预约!')", true);
                        return;
                    }
                    #endregion
                    break;
                case "2":
                    #region 上午三四节
                    #region 先检查该状态的实验室是否被预约
                    query = LMSDataContext.GetLMSDB.TB_lab.Where(l => l.labId == ddlLab.SelectedValue && l.labStatus34 == true);
                    #endregion
                    if (query.Count() == 0)
                    {
                        //表示该实验室在上午三四节可以被预约 
                        tb_lab.labStatus34 = true;
                        tb_lab.labStuNo = Int32.Parse(txtStuTotalCount.Text.Trim());
                        LMSDataContext.GetLMSDB.SubmitChanges();

                        #region 将实验室ID,预约时间,预约实验时间，教师ID，预约说明写入TB_orderLab数据表
                        WriteInfoTB_OrderLab();
                        #endregion

                        ClientScript.RegisterStartupScript(this.GetType(), "", "alert('预约成功!')", true);
                    }
                    else
                    {
                        //表示该实验室在上午三四节已经被预约了，不能冲突预约
                        ClientScript.RegisterStartupScript(this.GetType(), "", "alert('该实验室在上午三四节课已被预约!')", true);
                        return;
                    }
                    #endregion
                    break;
                case "3":
                    #region 下午一二节
                    #region 先检查该状态的实验室是否被预约
                    query = LMSDataContext.GetLMSDB.TB_lab.Where(l => l.labId == ddlLab.SelectedValue && l.labStatus56 == true);
                    #endregion
                    if (query.Count() == 0)
                    {
                        //表示该实验室在下午一二节可以被预约 
                        tb_lab.labStuNo = Int32.Parse(txtStuTotalCount.Text.Trim());
                        tb_lab.labStatus56 = true;
                        LMSDataContext.GetLMSDB.SubmitChanges();

                        #region 将实验室ID,预约时间,预约实验时间，教师ID，预约说明写入TB_orderLab数据表
                        WriteInfoTB_OrderLab();
                        #endregion

                        ClientScript.RegisterStartupScript(this.GetType(), "", "alert('预约成功!')", true);
                    }
                    else
                    {
                        //表示该实验室在下午一二节课已被预约了，不能冲突预约
                        ClientScript.RegisterStartupScript(this.GetType(), "", "alert('该实验室在下午一二节课已被预约!')", true);
                        return;
                    }
                    #endregion
                    break;
                case "4":
                    #region 下午三四节
                    #region 先检查该状态的实验室是否被预约
                    query = LMSDataContext.GetLMSDB.TB_lab.Where(l => l.labId == ddlLab.SelectedValue && l.labStatus78 == true);
                    #endregion
                    if (query.Count() == 0)
                    {
                        //表示该实验室在下午三四节可以被预约 
                        tb_lab.labStatus78 = true;
                        tb_lab.labStuNo = Int32.Parse(txtStuTotalCount.Text.Trim());
                        LMSDataContext.GetLMSDB.SubmitChanges();

                        #region 将实验室ID,预约时间,预约实验时间，教师ID，预约说明写入TB_orderLab数据表
                        WriteInfoTB_OrderLab();
                        #endregion

                        ClientScript.RegisterStartupScript(this.GetType(), "", "alert('预约成功!')", true);
                    }
                    else
                    {
                        //表示该实验室在下午三四节课已被预约了，不能冲突预约
                        ClientScript.RegisterStartupScript(this.GetType(), "", "alert('该实验室在下午三四节课已被预约!')", true);
                        return;
                    }
                    break;
                    #endregion
                case "5":
                    #region 晚上一二节
                    #region 先检查该状态的实验室是否被预约
                    query = LMSDataContext.GetLMSDB.TB_lab.Where(l => l.labId == ddlLab.SelectedValue && l.labStatusNight == true);
                    #endregion
                    if (query.Count() == 0)
                    {
                        //表示该实验室在晚上一二节可以被预约 
                        tb_lab.labStatusNight = true;
                        tb_lab.labStuNo = Int32.Parse(txtStuTotalCount.Text.Trim());
                        LMSDataContext.GetLMSDB.SubmitChanges();

                        #region 将实验室ID,预约时间,预约实验时间，教师ID，预约说明写入TB_orderLab数据表
                        WriteInfoTB_OrderLab();
                        #endregion

                        ClientScript.RegisterStartupScript(this.GetType(), "", "alert('预约成功!')", true);
                    }
                    else
                    {
                        //表示该实验室在下午三四节课已被预约了，不能冲突预约
                        ClientScript.RegisterStartupScript(this.GetType(), "", "alert('该实验室在晚上一二节课已被预约!')", true);
                        return;
                    }
                    #endregion
                    break;
                default:
                    break;
            }
            #endregion

        }

        /// <summary>
        /// 将实验室ID,预约时间,预约实验时间，教师ID，预约说明写入TB_orderLab数据表
        /// </summary>
        private void WriteInfoTB_OrderLab()
        {
            TB_orderLab tb_orderLab = new TB_orderLab();
            tb_orderLab.labId = ddlLab.SelectedValue;
            tb_orderLab.orderId = "Or" + ddlLab.SelectedValue;
            tb_orderLab.orderTime = DateTime.Now;
            if (Calendar1.SelectedDate.ToString("yyyy-MM-dd") == "0001-01-01")
            {
                tb_orderLab.orderTestTime = DateTime.Now.ToString("yyyy-MM-dd") + ddlLabStatus.SelectedItem.Text;
            }
            tb_orderLab.orderTestTime = Calendar1.SelectedDate.ToString("yyyy-MM-dd") + ddlLabStatus.SelectedItem.Text;
            //if (Session["teacherID"] != null)
            //{
            //    tb_orderLab.teacherId = Session["teacherID"].ToString();
            //}
            tb_orderLab.teacherId = "cdu118";// Session["teacherID"].ToString();
            tb_orderLab.testDetail = txtContent.Text.Trim();
            LMSDataContext.GetLMSDB.TB_orderLab.InsertOnSubmit(tb_orderLab);
            LMSDataContext.GetLMSDB.SubmitChanges();
        }




    }
}